import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/message")
public class MessageServlet extends HttpServlet {
    private final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 从服务器获取数据.

        resp.setContentType("application/json; charset=utf8");
        List<Message> messageList = load();

        resp.getWriter().write(objectMapper.writeValueAsString(messageList));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取数据并解析
        Message message = objectMapper.readValue(req.getInputStream(),Message.class);
        // 保存数据.
        save(message);

        resp.setStatus(200);
        System.out.println("提交数据成功: from = " + message.getFrom() + ", to = " +
                message.getTo() + ", message = " + message.getMessage());
    }

    private void save(Message message) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            // 建立连接
            connection = DBUtil.getConnection();
            // 构造sql
            String sql = "insert into message values(?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1,message.getFrom());
            statement.setString(2,message.getTo());
            statement.setString(3,message.getMessage());
            // 执行 sql.
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();

        } finally {
            DBUtil.close(connection,statement,null);

        }

        // 关闭连接.
    }

    private List<Message> load() {
        List<Message> messages = new ArrayList<>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            // 建立连接
            connection = DBUtil.getConnection();
            // 构造sql
            String sql = "select * from message";
            statement = connection.prepareStatement(sql);

            // 执行 sql
            resultSet = statement.executeQuery();

            // 遍历结果集.
            while (resultSet.next()) {
                Message message = new Message();

                message.setFrom(resultSet.getString("from"));
                message.setTo(resultSet.getString("to"));
                message.setMessage(resultSet.getString("message"));

                messages.add(message);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接.
            DBUtil.close(connection,statement,resultSet);
        }
        return messages;
    }
}
